<template>
<div>
    <div>
		<img v-bind:src="src" v-on:click="resetimg"><br>
		name:<input type="text" v-model="user.username"><br>
		pwd:<input type="password" v-model="password"><br>
		验证码:<input type="text" v-model="user.captchaImg"><br>
		<button v-on:click="login">login</button>
	</div>
</div>
</template>
<script>
export default {
    data(){
        return {
            src:'',
			password:'cs2017br',
			user:{username:'chuanshi',password:'',clientType:'O',captchaImg:'',uuidImg:''}
        }
    },created:function(){
		this.resetimg();
	},methods:{
		resetimg(){
			var that=this;
			this.$ajax.post('/token/getPublicKey').then((res)=>{
				that.publicKey=res.data.data;
			});
			this.user.uuidImg=this.guid()
			this.src="/token/getRandomImg/"+this.user.uuidImg;
		},
		guid(){
		  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
              var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
              return v.toString(16);
          });
		},login(){
            var that=this;
			var encrypt = new JSEncrypt();
			encrypt.setPublicKey(this.publicKey);
			var key=encrypt.encrypt(this.password);
			this.user.password=key;
			this.$ajax.post('/token/getPCToken?1=1',this.user).then(function(res){
				if(res.data.errorCode==0){
					that.$store.commit('SET_ONLINE_USER',res.data.data);
					that.storage.setItem('user',JSON.stringify(res.data.data));
					that.$router.go('/');
				}else{
					alert(res.data.message)
				}
			});
		}
	}
}
</script>